<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\NodeModel;
use think\Db;


class Index extends Base
{
    public function index()
    {
        // 获取权限菜单
        $node = new NodeModel();
        $this->assign([
            'menu' => $node->getMenu(session('rule'))
        ]);

        return $this->fetch('/index');
//        return view('/admin/index');
    }

    /**
     * 后台默认首页
     * @return mixed
     */
    public function indexPage()
    {
        //用户分类
        $data = Db::table('wb_buser')->field('role')->select();
//        halt($data);
        foreach ($data as $k=>$v){
            $res[]= $v['role'];
        }
//        halt(array_count_values($res));
        $user = array_count_values($res);
        //商业用户
        $b_user =$user['2'];
        //普通用户
        $o_user = $user['1'];
        //求总用户
        $sum = $b_user + $o_user;
        //商业用户的百分比
        $bu_user =round($b_user/$sum*100,2);
        //普通用户的百分比
        $or_user = round($o_user/$sum*100,2);
//        halt($or_user);

        //活跃用户排行
        $uid_count = Db::query('SELECT COUNT( uid ) AS num, m.uid,u.username	FROM wb_message AS m LEFT JOIN wb_buser AS u ON m.uid=u.id GROUP BY	m.uid	ORDER BY	num DESC LIMIT 8');
//        halt($uid_count);
        foreach ($uid_count as $k=>$v){
            $mess_count[] =$v['num'];
            $mess_uid[] =$v['uid'];
            $mess_name[] =$v['username'];
        }
       // halt($mess_name);
        return $this->fetch('index',[
            'bu_user'=>$bu_user,
            'or_user'=>$or_user,
            'mess_count'=>$mess_count,
            'mess_uid'=>$mess_uid,
            'mess_name'=>$mess_name
        ]);

    }
}
